/* "Reanalyzing the Link between Democracy and Economic Development"*/
/* journal: International Area Studies Review
/* authors: "Pelke, Lars"*/
/* date: 2023-07-24*/
/* written under "Stata version 17  MP—Parallel Edition"*/

/**********************/
/*Clean and set memory*/
/**********************/
clear all
set matsize 5000
set more off

/******************/
/*Install programs*/
/******************/
cap ssc install xtabond2 
cap ssc install xtivreg2 
cap ssc install spmat 
cap ssc install spmack
cap ssc install parmest

/*********************/
/*Sets base directory*/
/*********************/
cd ""      										    /* Set base directory                         */
global limit=30                                                                             /* Evaluate effects 25 years after transition */
local repsBS=100                                                                            /* Number of bootstrap repetitions            */


/* "Reanalyzing the Link between Democracy and Economic Development"*/
/* authors: "Pelke, Lars"*/
/* date: 2021-11-26*/
/* written under "Stata version 17  MP—Parallel Edition"*/
                                                                  /* Number of years to plot */

/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/*********************DEFINE REQUIRED PROGRAMS THAT WILL BE USED DURING THE EXECUTION OF THIS DO FILE **************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/

capture program drop vareffects1
program define vareffects1, eclass

quietly: nlcom (effect1: _b[shortrun]) ///
	  (shortrun: _b[shortrun]) ///
	  (lag1: _b[lag1]) ///
	  (lag2: _b[lag2]) ///
	  (lag3: _b[lag3]) ///
	  (lag4: _b[lag4]) ///
	  , post

quietly: nlcom (effect2: _b[effect1]*_b[lag1]+_b[shortrun]) ///
	  (effect1: _b[effect1]) ///
	  (shortrun: _b[shortrun]) ///
	  (lag1: _b[lag1]) ///
	  (lag2: _b[lag2]) ///
	  (lag3: _b[lag3]) ///
	  (lag4: _b[lag4]) ///
	  , post

quietly: nlcom (effect3: _b[effect2]*_b[lag1]+_b[effect1]*_b[lag2]+_b[shortrun]) ///
	  (effect2: _b[effect2]) ///
	  (effect1: _b[effect1]) ///
	  (shortrun: _b[shortrun]) ///
	  (lag1: _b[lag1]) ///
	  (lag2: _b[lag2]) ///
	  (lag3: _b[lag3]) ///
	  (lag4: _b[lag4]) ///
	  , post
	  
quietly: nlcom (effect4: _b[effect3]*_b[lag1]+_b[effect2]*_b[lag2]+_b[effect1]*_b[lag3]+_b[shortrun]) ///
	  (effect3: _b[effect3]) ///
	  (effect2: _b[effect2]) ///
	  (effect1: _b[effect1]) ///
	  (shortrun: _b[shortrun]) ///
	  (lag1: _b[lag1]) ///
	  (lag2: _b[lag2]) ///
	  (lag3: _b[lag3]) ///
	  (lag4: _b[lag4]) ///
	  , post	  

local previous (effect4: _b[effect4])	(effect3: _b[effect3]) (effect2: _b[effect2]) (effect1: _b[effect1])  
	  
forvalues j=5(1)$limit{	  
local j1=`j'-1
local j2=`j'-2
local j3=`j'-3
local j4=`j'-4

quietly: nlcom (effect`j': _b[effect`j1']*_b[lag1]+_b[effect`j2']*_b[lag2]+_b[effect`j3']*_b[lag3]+_b[effect`j4']*_b[lag4]+_b[shortrun]) ///
	  `previous' ///
	  (shortrun: _b[shortrun]) ///
	  (lag1: _b[lag1]) ///
	  (lag2: _b[lag2]) ///
	  (lag3: _b[lag3]) ///
	  (lag4: _b[lag4]) ///
	  , post	  	
	  
local previous  (effect`j': _b[effect`j']) `previous'	  

}

ereturn display
end

/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/************************************ ESTIMATION PROCEDURES AND STORING RESULTS ************************************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/

use "data/vdem_data.dta"

sort country_id year

xtset country_id year


/* Generate y-variable for GDP per capita */

gen y = gdppc_ppp_bcbt_mean_log*100

drop if year < 1896


/* WG estimates */
quietly: xtreg y l(1/4).y dem i.year, fe r cluster(country_id)
nlcom (shortrun: _b[dem])  (lag1: _b[L.y])  (lag2: _b[L2.y])  (lag3: _b[L3.y])  (lag4: _b[L4.y]), post
vareffects1
parmest, format(estimate min95 max95) saving("outputs/Step1/impulse_var1", replace)

use "outputs/Step1/impulse_var1", clear
split parm, p("effect")
keep if parm2!=""
destring parm2, force replace
gen time=parm2-1
tsset time
save "outputs/Step1/data_Figure.dta", replace

********************************************************************************
/* Robustness: Additional Covariates */

use "data/vdem_data.dta"

sort country_id year

xtset country_id year


/* Generate y-variable for GDP per capita */

gen y = gdppc_ppp_bcbt_mean_log*100

drop if year < 1896


/* WG estimates */
quietly: xtreg y l(1/4).y dem i.year l(1/4).v2x_polyarchy l(1/4).Maddison2020_pop_mean_log l(1/4).v2svstterr l(1/4).v2clrspct l(1/4).gdppc_ppp_bcbt_growth_rate_5avg, fe cluster(country_id) robust
nlcom (shortrun: _b[dem])  (lag1: _b[L.y])  (lag2: _b[L2.y])  (lag3: _b[L3.y])  (lag4: _b[L4.y]), post
vareffects1
parmest, format(estimate min95 max95) saving("outputs/Step1/impulse_var1_robust", replace)

use "outputs/Step1/impulse_var1_robust", clear
split parm, p("effect")
keep if parm2!=""
destring parm2, force replace
gen time=parm2-1
tsset time

save "outputs/Step1/data_Figure_robust.dta", replace

